const {
    Keypair,
    Transaction,
    SystemProgram,
    LAMPORTS_PER_SOL,
} = require("@solana/web3.js");

const config = require('./config');
const { user1, user2 } = require('./0_keypair');

const SolChain = require('./solChain');

const sendTx = async () => {
    let fromKeypair = await user1();
    let toKeypair = await user2();

    console.log('fromKeypair: ', fromKeypair);

    //fromKeypair = Keypair.generate();

    //console.log('fromKeypair: ', fromKeypair);

    let transaction = new Transaction();

    transaction.add(
        SystemProgram.transfer({
            fromPubkey: fromKeypair.publicKey,
            toPubkey: toKeypair.publicKey,
            lamports: LAMPORTS_PER_SOL,
        }),
    );



    let solChain = new SolChain(config.devNodeUrl2);
    let balance = await solChain.getBalance(fromKeypair.publicKey);
    console.log('from balance: ', balance);
    let tobalance = await solChain.getBalance(toKeypair.publicKey);
    console.log('to balance: ', tobalance);

    let getLatestBlockhash = await solChain.getLatestBlockhash();
    transaction.recentBlockhash = getLatestBlockhash.blockhash;
    transaction.feePayer = fromKeypair.publicKey;



    transaction.sign(fromKeypair);
    let rawTx = transaction.serialize();


    let ret = await solChain.sendRawTransaction(rawTx);
    console.log('ret: ', ret);

    //ret: 2X6nJry6NXN9QWfjiQ3dQirJ677qSfFFT4n8UQfTyq135jV48cEpAJ8uesUVGdYbgfMpbZNqdWP7CBtjJM4VZMh8
}
const get_tx = async ()=> {
    let solChain = new SolChain(config.devNodeUrl2);
    let ret = "2X6nJry6NXN9QWfjiQ3dQirJ677qSfFFT4n8UQfTyq135jV48cEpAJ8uesUVGdYbgfMpbZNqdWP7CBtjJM4VZMh8"
    let tx = await solChain.getTransaction(ret);

    console.log('tx: ', tx);
}
const getbalance = async () => {
    let fromKeypair = await user1();
    let toKeypair = await user2();
    let solChain = new SolChain(config.devNodeUrl2);
    let balance = await solChain.getBalance(fromKeypair.publicKey);
    console.log('from balance: ', balance);
    let tobalance = await solChain.getBalance(toKeypair.publicKey);
    console.log('to balance: ', tobalance);

}
//sendTx();
//get_tx();
getbalance();